package com.yami.shop.admin.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yami.shop.bean.model.OneLevelDistributionGrant;
import com.yami.shop.bean.model.User;
import com.yami.shop.bean.model.UserFundDetail;
import com.yami.shop.bean.param.OneLevelDistributionGrantParam;
import com.yami.shop.bean.vo.UserFundDetailVo;
import com.yami.shop.common.annotation.SysLog;
import com.yami.shop.common.response.ServerResponseEntity;
import com.yami.shop.common.util.PageParam;
import com.yami.shop.security.admin.util.SecurityUtils;
import com.yami.shop.service.OneLevelDistributionGrantService;
import com.yami.shop.service.UserService;
import jakarta.validation.Valid;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 一级分销奖金发放记录 前端控制器
 * </p>
 *
 * @author jcd
 * @since 2024-08-22
 */
@RestController
@RequestMapping("/shop/oneLevelDistributionGrant")
public class OneLevelDistributionGrantController {
    @Autowired
    private OneLevelDistributionGrantService oneLevelDistributionGrantService;

    /**
     * 分页查询
     *
     * @param page     分页对象
     * @param fundDetail 用户地址管理
     * @return 分页数据
     */
    @PreAuthorize("@pms.hasPermission('shop:oneLevelDistributionGrant:page')")
    @GetMapping("/page")
    public ServerResponseEntity<IPage<UserFundDetail>> page(PageParam page, OneLevelDistributionGrantParam fundDetail) {
        LambdaQueryWrapper<OneLevelDistributionGrant> userFundDetailLambdaQueryWrapper = new LambdaQueryWrapper<>(fundDetail);

        //userFundDetailLambdaQueryWrapper.exists(StringUtils.isNotBlank(fundDetail.getUserName()),"(select null from tz_user u where u.user_id = e_user_fund_detail.uid and u.nick_name like '%"+fundDetail.getUserName()+"%')");
        userFundDetailLambdaQueryWrapper.like(StringUtils.isNotBlank(fundDetail.getOrderNumberLike()),OneLevelDistributionGrant::getOrderNumber,fundDetail.getOrderNumberLike());
        userFundDetailLambdaQueryWrapper.like(StringUtils.isNotBlank(fundDetail.getUserIdsLike()),OneLevelDistributionGrant::getUserIds,fundDetail.getUserIdsLike());

        userFundDetailLambdaQueryWrapper.orderByDesc(OneLevelDistributionGrant::getCreateTime);
        PageParam page1 = oneLevelDistributionGrantService.page(page,userFundDetailLambdaQueryWrapper);

        List<OneLevelDistributionGrant> records = page1.getRecords();

        List<OneLevelDistributionGrant> list1=new ArrayList<>();
        for (OneLevelDistributionGrant detail:records){
            OneLevelDistributionGrant vo=new OneLevelDistributionGrant();
            BeanUtil.copyProperties(detail,vo);


            list1.add(vo);
        }

        page1.setRecords(list1);
        return ServerResponseEntity.success(page1);
    }


    /**
     * 通过id查询用户地址管理
     *
     * @param id id
     * @return 单个数据
     */
    @PreAuthorize("@pms.hasPermission('shop:oneLevelDistributionGrant:info')")
    @GetMapping("/info/{id}")
    public ServerResponseEntity<OneLevelDistributionGrant> getById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(oneLevelDistributionGrantService.getById(id));
    }

    /**
     * 新增用户地址管理
     *
     * @return 是否新增成功
     */
    @SysLog("新增用户地址管理")
    @PostMapping
    @PreAuthorize("@pms.hasPermission('shop:oneLevelDistributionGrant:save')")
    public ServerResponseEntity<Boolean> save(@RequestBody @Valid OneLevelDistributionGrant oneLevelDistributionGrant) {
        Long userId = SecurityUtils.getSysUser().getUserId();

        oneLevelDistributionGrant.setCreateTime(new Date());
        oneLevelDistributionGrant.setCreateBy(userId);
        oneLevelDistributionGrant.setDelFlag(0);

        return ServerResponseEntity.success(oneLevelDistributionGrantService.save(oneLevelDistributionGrant));
    }

    /**
     * 修改用户地址管理
     *
     * @return 是否修改成功
     */
    @SysLog("修改用户地址管理")
    @PutMapping
    @PreAuthorize("@pms.hasPermission('shop:oneLevelDistributionGrant:update')")
    public ServerResponseEntity<Boolean> updateById(@RequestBody @Valid OneLevelDistributionGrant oneLevelDistributionGrant) {
        Long userId = SecurityUtils.getSysUser().getUserId();

        oneLevelDistributionGrant.setUpdateBy(userId);
        oneLevelDistributionGrant.setUpdateTime(new Date());

        return ServerResponseEntity.success(oneLevelDistributionGrantService.updateById(oneLevelDistributionGrant));
    }

    /**
     * 通过id删除用户地址管理
     *
     * @param id id
     * @return 是否删除成功
     */
    @SysLog("删除用户地址管理")
    @DeleteMapping("/{id}")
    @PreAuthorize("@pms.hasPermission('shop:oneLevelDistributionGrant:delete')")
    public ServerResponseEntity<Boolean> removeById(@PathVariable("id") Long id) {
        return ServerResponseEntity.success(oneLevelDistributionGrantService.removeById(id));
    }
}

